﻿var refreshGirdData; // 更新数据

var companyId = '';
var kjnd = '';

var userdefault = {};
var alreadyformat = false;

var params = {};
var bootstrap = function ($, learun) {

    var pageWidth = 1024;

    "use strict";
    var page = {
        init: function () {
            userdefault = learun.clientdata.get(['usersetting']);
            //初始化打印插件
            hiprint.init();
            pageWidth = ($('.lr-layout-grid').width());
            page.bind();
            page.initGrid();
            page.search(params);
        },
        bind: function () {
            $('#company_select').lrselect({
                type: 'tree',
                // placeholder: '请选择机构',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                //allowSearch: true,
                select: function (item) {
                    if (item && item.value != '-1') {
                        companyId = item.value;
                    }
                }
            });
            $('#company_select').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            }).lrselectSet(userdefault.companyId);

            // 查询
            $('#btn_Search').on('click', function () {
                page.search(params);
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });

            //打印
            $('#lr_print').on('click', function () {
                var hiprintTemplate = null;
                learun.httpAsyncGet2(config.base_server() + 'printTempletes/findByCode', "&companyId=" + companyId + '&ztId=' + ztId + '&code=cnyeb', function (res) {
                    if (res.resp_code == 200) {
                        hiprintTemplate = new hiprint.PrintTemplate({
                            template: JSON.parse(res.data.templete),
                            paginationContainer: '.hiprint-printPagination'
                        });

                        //hiprintTemplate.design('#hiprint-printTemplate');

                        var rowdatas = $('#gridtable').jfGridGet('rowdatas');
                        var item = {};
                        item.pzflList = rowdatas;
                        if (!alreadyformat) {
                            for (var i = 0; i < item.pzflList.length; i++) {
                                item.pzflList[i].ncye = $.formatMoney(item.pzflList[i].ncye, 2);
                                item.pzflList[i].qcye = $.formatMoney(item.pzflList[i].qcye, 2);
                                item.pzflList[i].jfs = $.formatMoney(item.pzflList[i].jfs, 2);
                                item.pzflList[i].dfs = $.formatMoney(item.pzflList[i].dfs, 2);
                                item.pzflList[i].jlj = $.formatMoney(item.pzflList[i].jlj, 2);
                                item.pzflList[i].dlj = $.formatMoney(item.pzflList[i].dlj, 2);
                                item.pzflList[i].ye = $.formatMoney(item.pzflList[i].ye, 2);
                            }
                        }
                        alreadyformat = true;
                        item.ztName = $("#zt_select .lr-select-placeholder").html();
                        var pzrqStart = $("#pzrqStart").val();
                        var pzrqEnd = $("#pzrqEnd").val();
                        item.pzrq = pzrqStart.substring(0, 10) + "至于" + pzrqEnd.substring(0, 10);
                        item.fj = 1;
                        // hiprintTemplate.print(item);
                        hiprintTemplate.resetPage();
                        $('#myModal .modal-body .prevViewDiv').html(hiprintTemplate.getHtml(item));
                        $('#myModal').modal('show');
                    }
                });
                $('#A4_printByHtml').click(function () {
                    hiprintTemplate.printByHtml($('#myModal .modal-body .prevViewDiv'));
                })
            });

            //导出Excel
            $("#lr_export_excel").on('click', function () {
                if (params.companyId == undefined) {
                    learun.alert.warning('请先查询要导出的数据！');
                    return;
                }
                params.access_token = config.getToken().access_token;
                learun.download({
                    url: config.base_server() + 'billReports/trainingUsage/excel',
                    param: params
                });
            });
            //导出pdf
            $("#lr_export_pdf").on('click', function () {
                if (params.companyId == undefined) {
                    learun.alert.warning('请先查询要导出的数据！');
                    return;
                }
                params.access_token = config.getToken().access_token;
                learun.download({
                    url: config.base_server() + 'reports/cnyeb/pdf',
                    param: params
                });
            });
        },
        initGrid: function () {
            $("#gridtable").height($(window).height() - 170);
            $('#gridtable').jfGrid({
                url: config.base_server() + 'billReports/trainingUsage/list?access_token=' + config.getToken().access_token,
                headData: [
                    {label: '记账日期及凭证号', name: 'pzh', width: 120, align: 'center'},
                    {label: '凭证摘要', name: 'zy', width: 180, align: 'left'},
                    {
                        label: '报销金额', name: 'reimburseMoney', width: 100, align: 'right',
                        formatter: function (cellvalue) {
                            return $.formatMoney(cellvalue, 2)
                        }
                    },
                    {label: '出差方式', name: 'BusinesstripWay', width: 80, align: 'left'},
                    {label: '资金来源', name: 'zjly', width: 90, align: 'left'},
                    {label: '自培/委培/参培', name: 'joinCategory', width: 90, align: 'left'},
                    {label: '培训名称', name: 'name', width: 100, align: 'left'},
                    {label: '培训审批文件（文号）', name: 'fileAuthCode', width: 100, align: 'left'},
                    {label: '培训通知文件（文号）', name: 'fileNotifyCode', width: 100, align: 'left'},
                    {label: '培训场所(地点)', name: 'site', width: 100, align: 'left'},
                    {label: '实际天数', name: 'days', width: 60, align: 'center'},
                    {label: '参培人数', name: 'persons', width: 60, align: 'center'},
                    {label: '委培方式', name: 'consignWay', width: 80, align: 'center'},
                    {label: '受委托中介名称', name: 'consignUnitName', width: 100, align: 'left'},
                    {label: '参培组织单位名称', name: 'joinUnitName', width: 100, align: 'left'},
                    {
                        label: "费用支出明细", name: "费用支出明细", width: 900, align: 'center',
                        children: [
                            {
                                label: "小计", name: "total", width: 100, align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                            {
                                label: "师资费", name: "teacherFee", width: 100, align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                            {
                                label: "住宿费", name: "hotelFee", width: 100, align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                            {
                                label: "伙食费", name: "mealFee", width: 100, align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                            {
                                label: "培训场地费", name: "siteFee", width: 100, align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                            {
                                label: "交通费", name: "trafficFee", width: 100, align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                            {
                                label: "其他费", name: "otherFee", width: 100, align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            },
                            {
                                label: "培训费（参培）", name: "joinFee", width: 100, align: "right",
                                formatter: function (cellvalue) {
                                    return $.formatMoney(cellvalue, 2)
                                }
                            }
                        ]
                    },
                    {label: '备注', name: 'remark', width: 100, align: 'left'}
                ],
                dblclick: function (row) {
                    if (row.kjkmCode) {
                        var pzrqStart = $("#pzrqStart").val();
                        var pzrqEnd = $("#pzrqEnd").val();

                        var kjkmCode = row.kjkmCode;
                        var kjkmName = row.kjkmName;
                        learun.frameTab.open({
                            id: 'cnrjz_view' + kjkmCode,
                            css: 'fa fa-file-text-o',
                            name: '查看日记账' + kjkmCode + '_' + kjkmName,
                            path: config.webportal_server + 'pages/finance/report/cnrjzReport2.html?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&kjkm=' + kjkmCode + '&kjkmName=' + encodeURI(encodeURI(kjkmName)) + '&pzrqStart=' + pzrqStart + '&pzrqEnd=' + pzrqEnd + '&access_token=' + config.getToken().access_token
                        });
                    }
                },
                reloadSelected: true,
                mainId: 'code'
            });
        },
        search: function (param) {
            param = param || {};
            var pzrqStart = $("#pzrqStart").val();
            var pzrqEnd = $("#pzrqEnd").val();

            param.companyId = companyId;
            param.kjnd = kjnd;

            params = param;
            $('#gridtable').jfGridSet('reload', param);

            $("#pzrq_title").html(pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10));
        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    };

    page.init();
}


